package util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author Antony
 * @Since 2017/12/22 17:03
 */
public class DateUtil {
    public static final String SDF_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static Date string2Date(String date, String pattern) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date d = sdf.parse(date);
        return d;
    }

    public static Date string2Date(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(SDF_YYYY_MM_DD_HH_MM_SS);
        return sdf.parse(date);
    }


    /**
     * 将日期按小时滚动(前后都可以)
     * @param date
     * @param amount
     * @return
     */
    public static Date rollByHour(Date date, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, amount);
        return calendar.getTime();
    }

    /**
     * 获取当前时间
     * @return
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当天0点时间
     * @return
     */
    public static Date getCurrentZeroTime() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获取当年1月1日0点
     * @return
     */
    public static Date getCurrentZeroYear(){
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    public static Date getDefaultDate(){
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, 1971);
        c.set(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }


}
